package com.personalkeeper;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

public class Marker extends OverlayItem implements Parcelable, Cloneable {
    private int markerType;
    private Context context;//specify the context to get resources from

    public Marker(GeoPoint point, String title, String snippet, int type, Context context) {
        super(point, title, snippet);
        this.markerType = type;
        this.context = context;
    }

    @Override
    public Drawable getMarker(int stateBitset) {
        Drawable drawable = this.context.getResources().getDrawable(R.drawable.passed);
        //specify which marker to use
        switch (markerType) {
        case(Marker.TYPE_CURRENT_POINT):
            drawable = this.context.getResources().getDrawable(R.drawable.current);
            break;
        case(Marker.TYPE_PASSED_POINT):
            drawable = this.context.getResources().getDrawable(R.drawable.passed);
            break;
        case(Marker.TYPE_LOCATION_POINT):
            drawable = this.context.getResources().getDrawable(R.drawable.note);
            break;
        case(Marker.TYPE_CONTACT_POINT):
            drawable = this.context.getResources().getDrawable(R.drawable.contact);
            break;
        default:
            drawable = this.context.getResources().getDrawable(R.drawable.icon);
        }
        //set drawable state
        drawable.setState(new int[] {stateBitset});
        //set item's bounds and attach center-bottom point to map coordinates
        drawable.setBounds(-drawable.getIntrinsicWidth() / 2, -drawable.getIntrinsicHeight(), drawable.getIntrinsicWidth() / 2, 0);
        return drawable;
    }

    @Override
    public Marker clone() {
        Marker copy = new Marker(getPoint(), getTitle(), getSnippet(), markerType, context);
        return copy;
    }

    public void setType(int type) {
        markerType = type;
    }
    public int GetType() {
        return this.markerType;
    }
    public static final int TYPE_CURRENT_POINT = 100;
    public static final int TYPE_PASSED_POINT = 200;
    public static final int TYPE_LOCATION_POINT = 300;
    public static final int TYPE_CONTACT_POINT = 400;
    public Context getAssotiatedContext() {
        return this.context;
    }
    public void setAssotiateContext(Context context) {
        this.context = context;
    }

    @Override
    public int describeContents() {
        return this.hashCode();
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mPoint.getLatitudeE6());
        dest.writeInt(mPoint.getLongitudeE6());
        dest.writeString(mTitle);
        dest.writeString(mSnippet);
        dest.writeInt(markerType);
    }
    public Marker(Parcel source) {
        super(new GeoPoint(source.readInt(), source.readInt()),
              source.readString(), source.readString());
        this.markerType = source.readInt();

    }

    public static final Parcelable.Creator<Marker> CREATOR = new Parcelable.Creator<Marker>() {

        @Override
        public Marker createFromParcel(Parcel source) {
            return new Marker(source);
        }

        @Override
        public Marker[] newArray(int size) {
            return new Marker[size];
        }
    };

}